package com.reins.bookstore.serviceimpl;

import com.reins.bookstore.dao.OrderDao;
import com.reins.bookstore.dao.UserDao;
import com.reins.bookstore.entity.User;
import com.reins.bookstore.entity.UserAuth;
import com.reins.bookstore.object.Filter;
import com.reins.bookstore.object.OrderDTO;
import com.reins.bookstore.object.UserDTO;
import com.reins.bookstore.object.UserRate;
import com.reins.bookstore.service.OrderService;
import com.reins.bookstore.service.UserService;
import com.reins.bookstore.utils.sortUtils.sortUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderService orderService;

    @Override
    public UserAuth checkUser(String username, String password){
        UserAuth res = userDao.checkUser(username,password);
        return res;
    }

    @Override
    public UserDTO editUser(UserDTO user_dto, String password){
        UserAuth exist = userDao.findUserAuthById(user_dto.getUserId());
        if (exist == null)  // 不存在该用户，或者修改了id，非法
            return null;
        else                // 存在该用户，则其他属性都可以修改
        {
            return userDao.editUser(user_dto,password);
        }
    }

    @Override
    public UserDTO editUser(UserDTO user_dto){
        UserAuth exist = userDao.findUserAuthById(user_dto.getUserId());
        if (exist == null)  // 不存在该用户，或者修改了id，非法
        {
            return null;
        }
        else                // 存在该用户，则其他属性都可以修改
        {
            String password = exist.getPassword();
            return userDao.editUser(user_dto,password);
        }
    }

    @Override
    public List<UserDTO> findAllUsers(){
        return userDao.findAllUserDTOs();
    }

    @Override
    public List<UserAuth> findUserAuthInRange(Integer startId, Integer endId) {
        return userDao.findUserAuthInRange(startId, endId);
    }

    @Override
    public  List<UserAuth> getAllUsersSortedByType() {
        List<UserAuth> userAuthList = userDao.findAllUserAuths();
        return sortUtils.sortUserAuthByType(userAuthList);
    }

    @Override
    public UserAuth createUser(UserDTO user_dto, String password){
        UserAuth res = new UserAuth();

        if (findUserAuthByName(user_dto.getUsername()) == null)
            return userDao.createUser(user_dto,password);
        else
        {
            res.setUserId(0);
            return res;
        }
    }

    @Override
    public Integer deleteUser(Integer userId){
        User get = userDao.findUserById(userId);
        if (get == null)
            return 0;
        else{
            userDao.deleteUser(userId);
            return 1;
        }
    }
    @Override
    public UserAuth findUserAuthByName(String usernname){
        return userDao.findUserAuthByName(usernname);
    }

    @Override
    public UserAuth findUserById(Integer userId){
        return userDao.findUserAuthById(userId);
    }

    @Override
    public List<UserRate> getUserRate(Filter filter){


        List<UserDTO> users = findAllUsers();
        List<UserRate> res = new ArrayList<>();
        List<OrderDTO> orderdtos = new ArrayList<>();
       for (UserDTO user: users){

            filter.setUserId(user.getUserId());
            orderdtos = orderService.findAllOrders(filter);
            BigDecimal cost = new BigDecimal(0.0);
            for (OrderDTO orderdto : orderdtos){
                cost = cost.add(orderdto.getTotalorderCost());
            }
            res.add(new UserRate(user,cost));
        }
        // 开始排序
        Collections.sort(res, new Comparator<UserRate>() {
            @Override
            public int compare(UserRate o1, UserRate o2) {
                UserRate user1 = (UserRate) o1;
                UserRate user2 = (UserRate) o2;
                return user2.getCost().compareTo(user1.getCost());
            }
        });
        int i = 1;
        for (UserRate one_rate : res){
            one_rate.setRate(i);
            i++;
        }
        return res;
    }
    public UserAuth changeBanned(Integer id){
        UserAuth change = userDao.findUserAuthById(id);
        if (change.getBanned() == 1)
            change.setBanned(0);
        else
            change.setBanned(1);
        return userDao.saveAuthAndFlush(change);
    }

    public UserAuth changeType(Integer id){
        UserAuth change = userDao.findUserAuthById(id);
        if (change.getUserType() == 1)
            change.setUserType(0);
        else
            change.setUserType(1);
        return userDao.saveAuthAndFlush(change);
    }

}
